﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Framework;

namespace QuanLyThuVien_2
{
    public enum DIRECTION
    {
        TOP, RIGHT, BOTTOM, LEFT
    }
    public partial class frmMainMenu : Form
    {
        public static DIRECTION direction;
        public static int MAX_WIDTH;
        public static int MAX_HEIGHT;
        private int Form_Grow_X;
        private int Form_Grow_Y;
        private int Num_Grow;
        private Panel pnlSearch;
        private Panel pnlListMenu;
        private List<myScreen> List_myScreen;
        private myScreen currScreen;
        private ScreenSach screenSach;
        private ScreenPhieuMuon screenPhieuMuon;
        private ScreenDocGia screenDocGia;
        private ScreenNhanSu screenNhanSu;
        private ScreenTacGia screenTacgia;
        private ScreenNhaXuatBan screenNhaXuatBan;
        private ScreenPhieuTra screenPhieuTra; 
        private List<string> lstMenu;
        private List<string> lstImageBackgroundMenu;
        private List<PictureBox> lstPictureMenu;
        private int DistanceX;
        private int DistanceY;
        private int NumMove;
        private int IndexScreen;
        private int SelectScreen;


        private bool isDragging;
        private Point StartMouseDragPos;
        private Point MouseDragPos;

        public frmMainMenu()
        {
            InitializeComponent();
            MAX_WIDTH = 1000;
            MAX_HEIGHT = 650;
            Num_Grow = 20;
            //this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.Size = new Size(MAX_WIDTH, MAX_HEIGHT);
            this.MaximizeBox = false;
            Form_Grow_X = MAX_WIDTH/Num_Grow;
            Form_Grow_Y = MAX_HEIGHT / Num_Grow;
            List_myScreen = new List<myScreen>();
            DistanceX = 40;
            DistanceY = 20;
            NumMove = (this.Width - 200) / DistanceX;
            IndexScreen = 0;
            lstMenu = new List<string>();
            lstImageBackgroundMenu = new List<string>();
            lstPictureMenu = new List<PictureBox>();
            string line;
            StreamReader srd = new StreamReader("ListBackgroundMenu.txt");
            while ((line = srd.ReadLine()) != null)
            {
                lstImageBackgroundMenu.Add(line);
            }
        }

        private void frmMainMenu_Load(object sender, EventArgs e)
        {
            AddSearchToolbar();
            AddListMenu();
            AddScreen();
            for (int i = 0; i < List_myScreen.Count; i++)
            {
                List_myScreen[i].Pnl.MouseDown += new MouseEventHandler(Pnl_MouseDown);
                List_myScreen[i].Pnl.MouseMove += new MouseEventHandler(Pnl_MouseMove);
                List_myScreen[i].Pnl.MouseUp += new MouseEventHandler(Pnl_MouseUp);
                List_myScreen[i].Pnl.MouseEnter += new EventHandler(Pnl_MouseEnter);
                List_myScreen[i].Pnl.MouseWheel += new MouseEventHandler(Pnl_MouseWheel);
                this.Controls.Add(List_myScreen[i].Pnl);
            }
           
        }
        private void AddSearchToolbar()
        {
            pnlSearch = new Panel();
            pnlSearch.Name = "pnlSearch";
            pnlSearch.Location = new Point(0, 0);
            pnlSearch.Size = new Size(200, MAX_HEIGHT / 7);
            pnlSearch.BackColor = Color.FromArgb(222, 38, 38, 38);
            Label lbl = Util.CreateLabel("lblSearch", "Tìm kiếm ( Theo mã )", new Font("Segoe UI", 12, FontStyle.Regular, GraphicsUnit.Point), Color.Transparent, Color.White, new Size(pnlSearch.Width, pnlSearch.Height / 4), new Point(5, pnlSearch.Height / 5));

            TextBox txt = Util.CreateTextBox("txtSearch",new Font("Segoe UI", 17, FontStyle.Regular, GraphicsUnit.Pixel), new Size(pnlSearch.Width - pnlSearch.Width / 4, 22), new Point(5, lbl.Height * 2));

            Button btn = Util.CreateButton("btnSearch","", Image.FromFile(@"imgMenu/Search.png"), new Size(txt.Height + 1, txt.Height + 1), new Point(txt.Location.X + txt.Width, txt.Location.Y));

            pnlSearch.Controls.Add(btn);
            pnlSearch.Controls.Add(txt);
            pnlSearch.Controls.Add(lbl);


            this.Controls.Add(pnlSearch);
        }
        private void AddListMenu()
        {
            pnlListMenu = new Panel();
            pnlListMenu.Name = "pnlMenu";
            pnlListMenu.Location = new Point(0, pnlSearch.Height);
            pnlListMenu.Size = new Size(200, this.Height - this.Height/7);
            //pnlListMenu.BackColor = Color.FromArgb(222, 38, 38, 38);
            pnlListMenu.BackColor = Color.FromArgb(222, 38, 38, 38); ;

            for (int i = 0; i < lstImageBackgroundMenu.Count; i++)
            {
                PictureBox ptx = Util.CreatePictureBox("ptx" + i.ToString(), Image.FromFile(@"imgMenu/" + lstImageBackgroundMenu[i]), Color.Transparent, new Size(Image.FromFile(@"imgMenu/" + lstImageBackgroundMenu[i]).Width, Image.FromFile(@"imgMenu/" + lstImageBackgroundMenu[i]).Height), new Point(0, i * Image.FromFile(@"imgMenu/" + lstImageBackgroundMenu[i]).Height));
                ptx.Click += new EventHandler(ptx_Click);
                lstPictureMenu.Add(ptx);
                pnlListMenu.Controls.Add(ptx);

            }
            this.Controls.Add(pnlListMenu);
        }
        private void tmScreen_Tick(object sender, EventArgs e)
        {
            if (NumMove != 0)
            {
                if (MoveMenu(direction))
                    NumMove--;
                else
                    tmScreen.Enabled = false;
            }
            else
            {

                if (NumMove == 0)
                {
                    IndexScreen = SelectScreen;
                    tmScreen.Enabled = false;
                    currScreen = List_myScreen[IndexScreen];
                }
            }
        }
        private bool MoveMenu(DIRECTION direct)
        {
            if (direct == DIRECTION.LEFT)
            {
                foreach (myScreen screen in List_myScreen)
                    screen.Pnl.Location = new Point(screen.Pnl.Location.X - DistanceX, screen.Pnl.Location.Y);
                return true;

            }
            else if (direct == DIRECTION.RIGHT)
            {
                foreach (myScreen screen in List_myScreen)
                    screen.Pnl.Location = new Point(screen.Pnl.Location.X + DistanceX, screen.Pnl.Location.Y);
                return true;
            }
            else if (direct == DIRECTION.TOP)
            {
                foreach (Control ctrl in List_myScreen[IndexScreen].Pnl.Controls)
                    ctrl.Location = new Point(ctrl.Location.X, ctrl.Location.Y - DistanceY);
                return true;
            }
            else if (direct == DIRECTION.BOTTOM)
            {
                foreach (Control ctrl in List_myScreen[IndexScreen].Pnl.Controls)
                    ctrl.Location = new Point(ctrl.Location.X, ctrl.Location.Y + DistanceY);
                return true;
            }
            return false;
        }
        private void AddScreen()
        {
           
            screenNhanSu = new ScreenNhanSu(new Size(MAX_WIDTH - 200, this.Height), new Point(200, 0));
            screenNhanSu.InitializeComponent();
            List_myScreen.Add(screenNhanSu);

            screenSach = new ScreenSach(new Size(MAX_WIDTH - 200, this.Height), new Point(List_myScreen[List_myScreen.Count - 1].Pnl.Location.X + List_myScreen[List_myScreen.Count - 1].Pnl.Width, 0));
            screenSach.InitializeComponent();
            List_myScreen.Add(screenSach);

            screenTacgia = new ScreenTacGia(new Size(MAX_WIDTH - 200, this.Height), new Point(List_myScreen[List_myScreen.Count - 1].Pnl.Location.X + List_myScreen[List_myScreen.Count - 1].Pnl.Width, 0));
            screenTacgia.InitializeComponent();
            List_myScreen.Add(screenTacgia);

            screenDocGia = new ScreenDocGia(new Size(MAX_WIDTH - 200, this.Height), new Point(List_myScreen[List_myScreen.Count - 1].Pnl.Location.X + List_myScreen[List_myScreen.Count - 1].Pnl.Width, 0));
            screenDocGia.InitializeComponent();
            List_myScreen.Add(screenDocGia);

            screenNhaXuatBan = new ScreenNhaXuatBan(new Size(MAX_WIDTH - 200, this.Height), new Point(List_myScreen[List_myScreen.Count - 1].Pnl.Location.X + List_myScreen[List_myScreen.Count - 1].Pnl.Width, 0));
            screenNhaXuatBan.InitializeComponent();
            List_myScreen.Add(screenNhaXuatBan);

            screenPhieuMuon = new ScreenPhieuMuon(new Size(MAX_WIDTH - 200, this.Height), new Point(List_myScreen[List_myScreen.Count - 1].Pnl.Location.X + List_myScreen[List_myScreen.Count - 1].Pnl.Width, 0));
            screenPhieuMuon.InitializeComponent();
            List_myScreen.Add(screenPhieuMuon);

            screenPhieuTra = new ScreenPhieuTra(new Size(MAX_WIDTH - 200, this.Height), new Point(List_myScreen[List_myScreen.Count - 1].Pnl.Location.X + List_myScreen[List_myScreen.Count - 1].Pnl.Width, 0));
            screenPhieuTra.InitializeComponent();
            List_myScreen.Add(screenPhieuTra);
        }
        private void ptx_Click(object sender, EventArgs e)
        {
            if (!tmScreen.Enabled)
            {
                foreach (PictureBox ptx in lstPictureMenu)
                {
                    ptx.BackColor = Color.Transparent;
                }
                PictureBox ptxClick = (PictureBox)sender;
                SelectScreen = int.Parse(ptxClick.Name.Substring(ptxClick.Name.Length - 1));
                NumMove = Math.Abs(myScreen.MAX_WIDTH * (SelectScreen - IndexScreen)) / DistanceX;
                if (!tmScreen.Enabled)
                {
                    if (SelectScreen > IndexScreen)
                        direction = DIRECTION.LEFT;
                    else
                        direction = DIRECTION.RIGHT;
                    tmScreen.Enabled = true;
                }
                ptxClick.BackColor = Color.Silver;
            }
        }
        private void Pnl_MouseEnter(object sender, EventArgs e)
        {
            Panel pnl = (Panel)sender;
            pnl.Focus();
        }
        private void Pnl_MouseDown(object sender, MouseEventArgs e)
        {
            if (!isDragging)
            {
                StartMouseDragPos = new Point(e.X, e.Y);
                isDragging = true;
            }
        }
        private void Pnl_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging)
            {
                if (!tmScreen.Enabled)
                {
                   if (e.X > StartMouseDragPos.X)
                    {
                        if (IndexScreen > 0)
                        {
                            SelectScreen = IndexScreen - 1;
                            NumMove = screenDocGia.Pnl.Width / DistanceX;
                            ptx_Click(lstPictureMenu[SelectScreen], new EventArgs());
                            direction = DIRECTION.RIGHT;
                            tmScreen.Enabled = true;
                        }
                        else
                            isDragging = false;
                    }
                    else if (e.X < StartMouseDragPos.X)
                    {
                        if (IndexScreen < List_myScreen.Count - 1)
                        {
                            SelectScreen = IndexScreen + 1;
                            NumMove = screenDocGia.Pnl.Width / DistanceX;
                            ptx_Click(lstPictureMenu[SelectScreen], new EventArgs());
                            direction = DIRECTION.LEFT;
                            tmScreen.Enabled = true;
                        }
                        else
                            isDragging = false;
                    }
                }
            }
        }
        private void Pnl_MouseUp(object sender, EventArgs e)
        {
            isDragging = false;
            StartMouseDragPos = new Point();
        }
        private void Pnl_MouseWheel(object sender, MouseEventArgs e)
        {
            if (!tmScreen.Enabled)
            {
                if (e.Delta > 0)
                {
                    direction = DIRECTION.BOTTOM;
                    NumMove = DistanceY;
                    tmScreen.Enabled = true;
                }
                else if (e.Delta < 0)
                {
                    direction = DIRECTION.TOP;
                    NumMove = DistanceY;
                    tmScreen.Enabled = true;
                }
            }
        }
    }
}

